<?php
require_once("includes/pagetop.php");
if(!isset($_SESSION['Allyfolio_User_ID']) || $_SESSION['Allyfolio_User_ID']==0)
{
	echo "ERROR|~|~|Please refresh the page and try again.";
}
else 
{
	$loginUserID=$_SESSION['Allyfolio_User_ID'];
	$message_status=$_REQUEST["message_status"];
	$input = $_GET["q"];
    $data = array();
    $qryFetchAllMessagedFriends="select af_users.*,tempchat.* from af_users, (select *,if(`from`='".$loginUserID."', `to`, `from`) as friendID from cometchat where (`from`='".$loginUserID."' or `to`='".$loginUserID."') and message_status='".$message_status."' order by sent desc) as tempchat where af_users.User_ID<>'".$loginUserID."'  and ((af_users.User_ID=tempchat.`from` and tempchat.`to`='".$loginUserID."' ) or (af_users.User_ID=tempchat.`to` and tempchat.`from`='".$loginUserID."')) group by af_users.User_ID order by sent desc";
    $arrFetchAllMessagedFriends = $pdodbobj->fetch_array($pdodbobj->query($qryFetchAllMessagedFriends,array()));
    if(count($arrFetchAllMessagedFriends)>0)
    {
		for($i=0; $i<count($arrFetchAllMessagedFriends) ; $i++)
		{
			$json = array();
	        $json['name'] = $arrFetchAllMessagedFriends[$i]['Full_Name'];
	        $json['value'] = $arrFetchAllMessagedFriends[$i]['User_ID'];
	        //$json['image'] = $row['user_photo'];
	        $data[] = $json;
		}
	}

    header("Content-type: application/json");
    echo json_encode($data);
}
?>